#include <iostream>
#include <cstdio>
#include <algorithm>
using namespace std;

typedef unsigned long long ll;
ll T;
ll year, mon, day, ho, mi, sec;

ll bit;
ll month[13] = {0, 31, 28, 31, 30, 31, 30, 31, 31, 30, 31, 30, 31};
ll month_bit[33];
bool isLeap(int year)
{
    return ((year % 400 == 0) || (year % 100 != 0 && year % 4 == 0));
}

int main()
{
    month_bit[1] = 1;
    for (int i = 2; i <= 32; i++)
    {
        month_bit[i] = month_bit[i - 1] * 2;
    }
    scanf("%lld", &T);
    ll temp = 0;
    while (T--)
    {
        scanf("%lld%lld%lld%lld%lld%lld%lld", &bit, &year, &mon, &day, &ho, &mi, &sec);
        //分钟处理
        sec += month_bit[bit] - 1;
        temp = sec / 60;
        sec %= 60;
        mi += temp;
        //小时处理
        temp = mi / 60;
        mi %= 60;
        ho += temp;
        //日处理
        temp = ho / 24; //草 temp = day / 24;
        ho %= 24;
        day += temp;
        if (mon == 2)
        {
            month[2] = (isLeap(year)) ? 29 : 28;
        }
        while (day > month[mon])
        {
            day -= month[mon];
            mon++;
            if (mon > 12)
            {
                mon = 1;
                year++;
            }
            if (mon == 2)
            {
                month[2] = (isLeap(year)) ? 29 : 28;
            }
        }
        printf("%lld %lld %lld %lld %lld %lld\n", year, mon, day, ho, mi, sec);
    }
    return 0;
}